<?
include('func_1.php');
include('func_db.php');
include('func_plug.php');

$time0 = microtime(true);

session_start();

$time1 = microtime(true);

// Database connction
try { @db_call(); } catch (Exception $e) {};

$time2 = microtime(true);

// Session initialization from Cookie
if (!$_SESSION['inited']) 
{
  $_SESSION['inited'] = True;

  $lang = $_COOKIE["lang"];
  $_SESSION["lang"] = $lang;

  $username = $_COOKIE["login"];
  $password = $_COOKIE["password"];

  if ($username) 
  {
    // Autologon
    try 
    {
      $user = db_logon($username, $password);
      $_SESSION["user"] = $user;
    } 
    catch (Exception $e) 
    {
      $user = "";
    }
  }
} 


$url = $_SERVER['REQUEST_URI'];

$page = strtok($url, "?");
$args = strtok("?");


$page = substr($page, 1);
if ($page == "") {
  $page = "news";
}


// Initialize language
$lang = $_SESSION["lang"];

if (strpos($args, "lang=") !== False)
{
  $lang = preg_replace('/.*lang=(\w+).*/', '$1', $args);
  $_SESSION["lang"] = $lang;
  setcookie("lang", $lang, time()+3600*24*365);
}

if (!$lang) {
  $lang = "en";
}

if ($_SESSION["msglang"] == $lang)
{
  $msg = $_SESSION["msg"];
}
else
{
  // Initialize localization
  include('localize.php');
  $_SESSION["msg"] = $msg;
  $_SESSION["msglang"] = $lang;
}


// Logon/Logoff
if (strpos($args, "logoff") !== False) 
{
  $user = "";
  $_SESSION["user"] = "";
  setcookie("login", "", time()+3600*24*365);
  setcookie("password", "", time()+3600*24*365);
} 
else 
{
  $user = $_SESSION['user'];

  if ($_SESSION["autologon"]) {
    $_SESSION["autologon"] = False;
    setcookie("login", $user["name"], time()+3600*24*365);
    setcookie("password", $user["password"], time()+3600*24*365);
  }
}


if ($user) 
{
  $curtheme = $user["theme"];
}


// id=...
if (strpos($args, "id=") !== False)
{
  $val = preg_replace('/.*id=(\d*).*/', '$1', $args);
}

switch ($page)
{
  case "getimage":
    include "get_image.php";
    break;

  case "getfile":
    include "get_file.php";
    break;

  default:
    include "main.php";
}


?>
